import { onBeforeUnmount } from 'vue'

export function useCountdown(time, offCountdown) {
	let timr = null
	
	function countdown() {
		timr = setInterval(() => {
			if (time.value <= 1) clearCountdown()
			else --time.value
		}, 1000)
	}
	
	function clearCountdown() {
		offCountdown && offCountdown()
		clearInterval(timr)
		timr = null
	}
	
	onBeforeUnmount(clearCountdown)
	
	return { countdown, clearCountdown }
}